<?php


namespace app\controller\Study;


use app\BaseController;
use app\Models\Study\StudentModel;
use think\facade\Db;

class StudentController extends BaseController
{

    /**
     * 获取学生列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getStudents()
    {
        //每页条数
        $size = (int)$this->request->param('size', 15);
        //页数
        $page = (int)$this->request->param('page', 1);
        $limit = ($page - 1) * $size;

        $students = Db::query("select * from `student` where `status`=1 order by created_at desc limit {$limit},{$size}");

        return json($students);
    }

    public function addStudents()
    {
        $nameArr = ["吴小明", "张红", "赵四", "王五", "钱学", "孙空", "李珍"];
        $ageArr = [18, 19, 20, 21, 22, 23, 24, 25];
        $saveArr = [];
        $createdAt = time();
        for ($i = 0; $i < 200; $i++) {
            $arr = [];
            $arr['name'] = $nameArr[array_rand($nameArr)];
            $arr['age'] = $ageArr[array_rand($ageArr)];
            $arr['id_number'] = "42011720100512" . mt_rand(1000, 9999);
            $arr['created_at'] = $createdAt;
            $arr['status'] = 1;
            $saveArr[] = $arr;
        }
        Db::table('student')->insertAll($saveArr);
        return json('添加成功');
    }

    /**
     * 添加学生接口
     * @return \think\response\Json
     */
    public function addStudent()
    {
        //接收请求过来的姓名字段
        $name = (string)$this->request->param('name');
        //接收请求过来的年龄字段
        $age = (int)$this->request->param('age');
        //接口请求过来的身份证字段
        $id_number = (string)$this->request->param('id_number');

        try {
            $student = new StudentModel();
            $student->name = $name;
            $student->age = $age;
            $student->id_number = $id_number;
            $student->created_at = time(); //时间戳，表示数据入库时间
            $student->status = 1;
            $student->save();
        } catch (\Exception $exception) {
            return json("学生数据入库失败" . $exception->getMessage(), 404);
        }
        return json("添加成功");
    }

    /**
     * 获取单个学生信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStudent()
    {
        $student_id = (int)$this->request->param('student_id');
        if (!$student_id) {
            return json('参数不合法', 404);
        }
        //查询单条数据
        $student = StudentModel::where('id', $student_id)->where('status', 1)->find();

        if (empty($student)) {
            return json('学生信息不存在', 404);
        }
        //把时间戳转化为可读的日期格式
        $student->created_at = date("Y-m-d H:i", $student->created_at);
        return json($student);
    }

    /**
     * 修改学生信息接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateStudent()
    {
        $student_id = (int)$this->request->param('student_id');
        //接收请求过来的姓名字段
        $name = (string)$this->request->param('name');
        //接收请求过来的年龄字段
        $age = (int)$this->request->param('age');
        //接口请求过来的身份证字段
        $id_number = (string)$this->request->param('id_number');

        if (!$student_id) {
            return json('参数不合法', 404);
        }
        //查询单条数据
        $student = StudentModel::where('id', $student_id)->where('status', 1)->find();
        if (empty($student)) {
            return json('学生信息不存在', 404);
        }
        try {
            $student->name = $name;
            $student->age = $age;
            $student->id_number = $id_number;
            $student->update_at = time(); //时间戳，表示数据更新时间
            $student->save();
        } catch (\Exception $exception) {
            return json("学生数据修改失败" . $exception->getMessage(), 404);
        }

        return json("更新成功");
    }

    /**
     * 删除学生
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteStudent()
    {
        $student_id = (int)$this->request->param('student_id');
        if (!$student_id) {
            return json('参数不合法', 404);
        }
        //查询单条数据
        $student = StudentModel::where('id', $student_id)->where('status', 1)->find();
        if (empty($student)) {
            return json('学生信息不存在', 404);
        }
        $student->status = 2;
        $student->save();

        return json("删除成功");
    }
}